﻿using System;
using System.Collections.Generic;
using System.Text;
using VietKinsNutrition.Data;
using System.Windows.Forms;
using System.Data;
using DevExpress.XtraEditors;
using VietKinsNutrition.Common;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.BandedGrid;

namespace VietKinsNutrition.Bussiness
{
    public class ClsThucdon_monanBLL : ClsBindingBLL
    {

        public void Bind_LoaimonanCbo(ComboBoxEdit Cbo)
        {
            //ClsTblDm_LoaimonDAL ClsTblDm_Loaimondal = new ClsTblDm_LoaimonDAL();
            // add Lựa chọn/
            DataTable Bs;
            ClsTblDm_LoaimonDAL ClsTblDm_Loaimondal = new ClsTblDm_LoaimonDAL();
            Bs = ClsTblDm_Loaimondal.SelectAll();
            Bs.Rows.Add(0, 0, "--Mục chọn--");
            foreach (DataRow Row in Bs.Rows)
            {
                Bs.Rows.Add(Row["STT"], Row["Id"], Row["Tenloaimon"]);
            }
        }
        public void BingSource_DataGridThucdontuan( AdvBandedGridView GridView, GridControl GrdControl,int KhoilopId, DateTime Value)
        {
            DataTable dtable = new DataTable();
            dtable.Columns.Add("BuaantrongngayId");
            dtable.Columns.Add("STT");
            dtable.Columns.Add("Tenbuaan");
            dtable.Columns.Add("Thu2");
            dtable.Columns.Add("Thu3");
            dtable.Columns.Add("Thu4");
            dtable.Columns.Add("Thu5");
            dtable.Columns.Add("Thu6");
            dtable.Columns.Add("Thu7");
            int k = 0;
            foreach (DataRow Rw in ClsThucdon_monandalAcc.ThucdontuanSelectAllByKhoilopIdandDate(KhoilopId, Value).Rows)
            {
                dtable.Rows.Add(k);
                for (int i = 0; i < Rw.ItemArray.Length; i++ )
                {
                    if (Rw[i] == null || Rw[i].ToString() == "")
                    {
                        Rw[i] = " ";
                    }
                    dtable.Rows[k][i] = Rw[i].ToString();
                }
                k++;
            }
            GridView.GridControl.DataSource = dtable;
            // bin lai ten cot;

            ReturnDay(Value);
            DateTime dayShow = Value;
            while (dayShow.DayOfWeek != DayOfWeek.Monday)
                dayShow = dayShow.AddDays(-1);
            for (int i = 0; i < 6; i++)
            {
                GridView.Columns[(i + 3)].Caption =
                    (dayShow.DayOfWeek == DayOfWeek.Monday ? "Thứ 2" :
                    dayShow.DayOfWeek == DayOfWeek.Tuesday ? "Thứ 3" :
                    dayShow.DayOfWeek == DayOfWeek.Wednesday ? "Thứ 4" :
                    dayShow.DayOfWeek == DayOfWeek.Thursday ? "Thứ 5" :
                    dayShow.DayOfWeek == DayOfWeek.Friday ? "Thứ 6" :
                    dayShow.DayOfWeek == DayOfWeek.Saturday ? "Thứ 7" : "CN") + ", " + dayShow.Day.ToString() + "-" + dayShow.Month.ToString() + "-" + dayShow.Year.ToString();
                dayShow = dayShow.AddDays(1);
            }

            //try
            //{
            //    int a = 0;
            //    for (int j = 3; j <= 8; j++)
            //    {
            //        a++;
            //        GridView.Columns[j].Caption = Value.AddDays(j - 3).Day.ToString() + "/" + Value.AddDays(j - 3).Month.ToString() + "/" + Value.AddDays(j - 3).Year.ToString();
            //    }
            //}
            //catch { }
        }

        private string[] _Time = new string[9];
        public void ReturnDay(DateTime InputDay)
        {
            int i = 0;
            DateTime dayShown = InputDay;
            while (dayShown.DayOfWeek != DayOfWeek.Monday)
                dayShown = dayShown.AddDays(-1);

            while (dayShown.DayOfWeek != DayOfWeek.Sunday)
            {
                _Time[i] = dayShown.Day.ToString() + "/" + dayShown.Month.ToString();
                i += 1;
                dayShown = dayShown.AddDays(1);
            }
        }

        public DataTable BingSource_DataGridTrunggian(int KhoilopId, DateTime Value)
        {

            return ClsThucdon_monandalAcc.ThucdontuanSelectAllByKhoilopIdandDate(KhoilopId, Value);

        }
        public void ThucdontuanSelectAllByBeginDateToEndDatime(GridView DG, List<DateTime> BgDate, List<DateTime> EndDate)
        {
            DG.GridControl.DataSource = ClsThucdon_monandalAcc.ThucdontuanSelectAllByBeginDateToEndDatime(BgDate, EndDate);
        }
        public DateTime ThucdontuanSelectAllByEndDatime()
        {
            DataSet set = new DataSet();
            set = ClsThucdon_monandalAcc.ThucdontuanSelectReturnEndDatime();
            DateTime ReturnDt = DateTime.Now;
            if (set.Tables[0].Rows.Count > 0)
            {
                try
                {
                    ReturnDt = DateTime.Parse(set.Tables[0].Rows[0]["ngay"].ToString());
                }
                catch { }
            }
            return ReturnDt;
        }
        public DataSet DataSet_ThucdontuanSelectAllByBeginDateToEndDatime(List<DateTime> BeginDate, List<DateTime> EndDate)
        {

            return ClsThucdon_monandalAcc.DataSet_ThucdontuanSelectAllByBeginDateToEndDatime(BeginDate, EndDate);
        }
        public void Bind_LophocIdCbo(int Id, ComboBoxEdit Cbo)
        {
            ClsTblLophocDAL ClsTblLophocdal = new ClsTblLophocDAL();
            //Cbo.DataSource = ClsTblLophocdal.SelectById(Id).Tables[0];
            //Cbo.ValueMember = "Id";
            //Cbo.DisplayMember = "Tenlop";
        }
        int _Id = 0;
        public void Bind_DotuoiIdCbo(int LopId, ComboBoxEdit Cbo)
        {
            DataTable Tbl;
            ClsTblLophocDAL ClsTblLophocdal = new ClsTblLophocDAL();
            Tbl = ClsTblLophocdal.SelectById(LopId).Tables[0];
            if (Tbl.Rows.Count != 0)
            {
                DataRow R = Tbl.Rows[0];
                _Id = int.Parse(R["DotuoiId"].ToString());
            }

            ClsTblDm_DotuoiDAL ClsTblDm_Dotuoidal = new ClsTblDm_DotuoiDAL();
            //Cbo.DataSource = ClsTblDm_Dotuoidal.SelectById(_Id).Tables[0];
            //Cbo.ValueMember = "Id";
            //Cbo.DisplayMember = "Tendotuoi";
        }
        public void Bind_BuaantrongngayCboAll(ComboBoxEdit Cbo)
        {
            ClsTblDm_BuaantrongngayDAL ClsTblDm_Buaantrongngaydal = new ClsTblDm_BuaantrongngayDAL();
            //Cbo.DataSource = ClsTblDm_Buaantrongngaydal.SelectAll();
            //Cbo.ValueMember = "Id";
            //Cbo.DisplayMember = "Tenbuaan";
        }

        public static DataTable GetAll()
        {
            return ClsTblDm_BuaantrongngayDAL.GetAll();
        }

        public void Bind_BuaantrongngayCbo(int Id, ComboBoxEdit Cbo)
        {
            ClsTblDm_BuaantrongngayDAL ClsTblDm_Buaantrongngaydal = new ClsTblDm_BuaantrongngayDAL();
            //Cbo.DataSource = ClsTblDm_Buaantrongngaydal.GetById(Id).Tables[0];
            //Cbo.ValueMember = "Id";
            //Cbo.DisplayMember = "Tenbuaan";
        }
        public void Del_TblThucdon_monan_DeleteByNamhocId(int NamhocId)
        {
            ClsThucdon_monandalAcc.Del_TblThucdon_monan_DeleteByNamhocId(NamhocId);
        }
        public void Del_Thucphamtheobua_DeleteByNamhocId(int NamhocId)
        {
            ClsThucdon_monandalAcc.Del_Thucphamtheobua_DeleteByNamhocId(NamhocId);
        }
        public void Bind_NhomthucphamCbo(ComboBoxEdit Cbo)
        {
            DataTable Bs;
            ClsTblDm_Nhomthucpham_ThucanDAL ClsTblDm_Nhomthucpham_Thucandal = new ClsTblDm_Nhomthucpham_ThucanDAL();
            Bs = ClsTblDm_Nhomthucpham_Thucandal.SelectAll();
            Bs.Rows.Add(0, -1, "--Mục chọn--");
            foreach (DataRow Row in Bs.Rows)
            {
                Bs.Rows.Add(Row["STT"], Row["Id"], Row["Tennhom"]);
            }
            UserComboBoxItem.SetItem(Cbo, Bs, "Id", "Tennhom");
        }
        public void Bind_ThuvienmonanGrid(int LoaimonId, int DotuoiId, DataGridView Grid)
        {
            ClsTblDm_ThuvienmonanDAL ClsTblDm_Thuvienmonandal = new ClsTblDm_ThuvienmonanDAL();
            Grid.DataSource = ClsTblDm_Thuvienmonandal.SelectAllByLoaimonId(LoaimonId, DotuoiId);
        }
        public void Bind_GridSelectAllByLoaimonIdandByName(int LoaimonId, string Tenmonan, DataGridView Grid)
        {
            ClsTblDm_ThuvienmonanDAL ClsTblDm_Thuvienmonandal = new ClsTblDm_ThuvienmonanDAL();
            Grid.DataSource = ClsTblDm_Thuvienmonandal.SelectAllByLoaimonIdandByName(LoaimonId, Tenmonan);
        }
        public void Bind_ThuvienmonanGrid(int LoaimonId, DataGridView Grid)
        {
            ClsTblDm_ThuvienmonanDAL ClsTblDm_Thuvienmonandal = new ClsTblDm_ThuvienmonanDAL();
            Grid.DataSource = ClsTblDm_Thuvienmonandal.SelectAllByLoaimonId(LoaimonId);
        }

        public void Bind_Thucpham_thucanGridByLoainhomthucphamIdandTen(int NhomthucanId, string Ten, DataGridView Grid)
        {
            ClsThucdon_monanDAL ClsThucdon_monandal = new ClsThucdon_monanDAL();
            Grid.DataSource = ClsThucdon_monandal.Thucpham_thucan_FindNameSamePrc(NhomthucanId, Ten).Tables[0];
        }
        public void Bind_ThuvienmonanByLoaimonanIdTenmonanDotuoiIdGrid(int LoaimonId, int DotuoiId, string Ten, DataGridView Grid)
        {
            ClsTblDm_ThuvienmonanDAL ClsTblDm_Thuvienmonandal = new ClsTblDm_ThuvienmonanDAL();
            Grid.DataSource = ClsTblDm_Thuvienmonandal.FindSameName(LoaimonId, DotuoiId, Ten).Tables[0];
        }
        public void Bind_Thucpham_thucanGrid(int LoainhomthucphamId, DataGridView Grid)
        {
            DataTable ReturnTbl = null;
            ClsTblMonan_ThucphamBLL ClsTblMonan_Thucphambll = new ClsTblMonan_ThucphamBLL(ReturnTbl);
            ClsTblMonan_Thucphambll.Bind_GridThucpham_thucan(LoainhomthucphamId, Grid);
        }
        public DataTable SelectByNgayanKhoilop(DateTime Ngay, int KhoilopId)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsThucdon_monandalAcc.SelectByNgayanKhoilop(Ngay, KhoilopId);
            return Tbl;
        }
        public bool SelectByNgayNX(DateTime Ngay)
        {
            bool Check = false;
            Check = ClsThucdon_monandalAcc.SelectByNgayNX(Ngay);
            return Check;
        }
        public DataTable SelectBy(DateTime Ngay)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsThucdon_monandalAcc.SelectBy(Ngay);
            return Tbl;
        }
        static ClsThucdon_monanDAL ClsThucdon_monandalAcc = new ClsThucdon_monanDAL();
        //
        public void TblThucdon_monan_Del_ByNgay_KhoilopId_BuaantrongngayId(DateTime Ngay, int BuaantrongngayId, int KhoilopId)
        {
            ClsThucdon_monandalAcc.TblThucdon_monan_Del_ByNgay_KhoilopId_BuaantrongngayId(Ngay, BuaantrongngayId, KhoilopId);
        }
        public DataTable TblThucdon_monan_ByNgay_KhoilopId_BuaantrongngayId(DateTime Ngay, int BuaantrongngayId, int KhoilopId)
        {
            return ClsThucdon_monandalAcc.TblThucdon_monan_ByNgay_KhoilopId_BuaantrongngayId(Ngay, BuaantrongngayId, KhoilopId);
        }
        public void SeletByBeginNgayToNgay(DateTime Beginngay, DateTime Tongay, DataGridView Grid)
        {
            Grid.DataSource = ClsThucdon_monandalAcc.SeletByBeginNgayToNgay(Beginngay, Tongay);
        }
        public void Bind_GridSeletByNgay(DateTime Ngay, DataGridView Grid)
        {
            Grid.DataSource = ClsThucdon_monandalAcc.SeletByNgay(Ngay);
        }
        public void Bind_Khoilop(ComboBoxEdit Cbo)
        {
            UserComboBoxItem.SetItem(Cbo, ClsThucdon_monandalAcc.Bind_Khoilop().Tables[0], "Id", "Tenkhoilop");
        }
        public DataTable Tbl_Khoilop()
        {
            return ClsThucdon_monandalAcc.Bind_Khoilop().Tables[0];
        }
        public static DataTable GetKhoilop()
        {
            return ClsThucdon_monandalAcc.Bind_Khoilop().Tables[0];
        }
        public static DataTable GetKhoilop(int khoilopid)
        {

            return ClsThucdon_monandalAcc.Bind_Khoilop(khoilopid).Tables[0];
        }
        public void Add(int ThuvienmonanId, DateTime Ngay, int BuaantrongngayId, int KhoilopId)
        {
            ClsThucdon_monandalAcc.Add(ThuvienmonanId, Ngay, BuaantrongngayId, KhoilopId);
        }
        public DataTable TblThucdon_monanByVar(DateTime Ngay, int BuaantrongngayId, int KhoilopId)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsThucdon_monandalAcc.TblThucdon_monanByVar(Ngay, BuaantrongngayId, KhoilopId);
            return Tbl;
        }
        public void Add(DateTime Ngay, int BuaantrongngayId, int KhoilopId, string Tenmonan)
        {
            ClsThucdon_monandalAcc.Add(Ngay, BuaantrongngayId, KhoilopId, Tenmonan);
        }
        public void Del(DateTime Ngay)
        {
            ClsThucdon_monandalAcc.Del(Ngay);
        }
        public DataTable SeletByBuaantrongngayId(int BuaantrongngayId)
        {
            return ClsThucdon_monandalAcc.SeletByBuaantrongngayId(BuaantrongngayId);
        }

        ClsTblLenthucphammonanchobuaDAL ClsTblLenthucphammonanchobuadal = new ClsTblLenthucphammonanchobuaDAL();
        public DataTable TblBaocaoduongchattrongbuaan(int KhoilopId, int BuaantrongngayId, DateTime Ngay)
        {
            return this.ClsTblLenthucphammonanchobuadal.TblBaocaoduongchattrongbuaan(KhoilopId, BuaantrongngayId, Ngay);
        }

        //TblLenthucphammonanchobuathucteByBaoanId
        public DataSet TblLenthucphammonanchobuathucteStart_ByBaoanId(int BaoanId)
        {
            return this.ClsTblLenthucphammonanchobuadal.TblLenthucphammonanchobuathucteByBaoanId(BaoanId);
        }
        public DataSet TblLenthucphammonanthucteByBaoanIdFillDongia(int BaoanId, DateTime NgayNX, int TrangthaiNX)
        {
            return this.ClsTblLenthucphammonanchobuadal.TblLenthucphammonanthucteByBaoanIdFillDongia(BaoanId, NgayNX, TrangthaiNX);
        }
        public DataSet TblLenthucphammonanchobuaByBaoanId(int BaoanId, DateTime NgayNX, int TrangthaiNX)
        {
            return this.ClsTblLenthucphammonanchobuadal.TblLenthucphammonanchobuaByBaoanIdFillDongia_Thanhtien(BaoanId, NgayNX, TrangthaiNX);
        }
        // Xu ly ngoai luong ***************************
        public static DataTable ReturnThucdon_monan;
        public static DataTable ReturnLenthucphambuaan;

        public void BuaantrongngayIdCbo(ComboBoxEdit Cbo)
        {
            DataSet Set;
            ClsTblDm_BuaantrongngayDAL ClsTblDm_Buaantrongngaydal = new ClsTblDm_BuaantrongngayDAL();
            Set = (DataSet)((BindingSource)ClsTblDm_Buaantrongngaydal.SelectAll()).DataSource;
            DataSet _Set = Set.Clone();
            _Set.Tables[0].Rows.Add(0, -1, "--Tất cả--", null);
            int i = 0;
            foreach (DataRow row in Set.Tables[0].Rows)
            {
                i = i + 1;
                _Set.Tables[0].Rows.Add(i, row["Id"], row["Tenbuaan"], row["Phanbotienan"]);

            }
        }

        public static DataTable GetByDate4Report(DateTime dateReport)
        {
            return ClsThucdon_monanDAL.GetByDate4Report(dateReport);
        }

        public static DataTable GetByWeek4Report(DateTime dateReport)
        {
            DateTime startDate = dateReport.AddDays(-(int)dateReport.DayOfWeek);
            DateTime endDate = startDate.AddDays(7);
            return ClsThucdon_monanDAL.GetByWeek4Report(startDate, endDate);
        }
    }
}
